home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / vlapak1.zip / INT9.ZIP / INT9.INC < prev    next >
Text File  |  1993-07-26  |  6KB  |  181 lines

  1. ────────────────────────────────────────────────────────────────────────────
  2. ;
  3. ;   This code was written by Draeden of VLA on June 28, 1993.
  4. ;
  5. ────────────────────────────────────────────────────────────────────────────    
  6. Active_Keys db  128 dup (0) ;correspond to the 127 possible key codes
  7.                             ; 0 means it's not pressed, nonzero, it is
  8. Key_Pressed db  ?           ;says a key was pressed.. user must zero out..
  9. ESC_Pressed db  ?           ;flag that says that the ESC key is pressed
  10. Last_Pressed db ?           ;scan code of last key pressed
  11. ────────────────────────────────────────────────────────────────────────────
  12.     ────────────────────────────────────────────────────────────────────
  13.     ;   This routine will wait until the keyboard's status port
  14.     ;   says it is ready..
  15.     ────────────────────────────────────────────────────────────────────
  16. PROC KB_Wait  NEAR
  17.     xor     cx,cx
  18. @@Wait_KB:
  19.     in      al,64h              ;keep on polling the status port until
  20.     test    al,2                ; the key is done coding...
  21.     loopnz  @@Wait_KB
  22.     ret
  23. ENDP
  24.  
  25.     ────────────────────────────────────────────────────────────────────
  26.     ;   A keyboard interrupt (irq 1, int 9) that replaces the old int 9
  27.     ;   This does not buffer keys or anything.. meant to be used in a
  28.     ;   game/demo type setting where you are only concerned with what
  29.     ;   keys are currently being pressed.
  30.     ;
  31.     ;   Limitations (of the hardware):
  32.     ;     ■ cannot distinguish between right and left CTRL & ALT keys
  33.     ;     ■ cannot distinguish between grey keys and non-grey keys
  34.     ;       I could, but that would take a little work... The grey keys 
  35.     ;       send a scan code that is the same as the LEFT SHIFT key and 
  36.     ;       then send the same scan code as the normal key
  37.     ;     ■ because of the above problem, the LEFT SHIFT key may stay on
  38.     ;       even though it's not being pressed- don't rely on that key...
  39.     ;       BUT, the 2ah will not be sent first if Num-Lock is OFF
  40.     ;
  41.     ; Variables:
  42.     ;
  43.     ;   Last_Pressed - holds the scan code of the last key serviced
  44.     ;                   whether it was turned on or off
  45.     ;   Key_Pressed  - contains a 1 if there has been a keypress since
  46.     ;                   the last time the user cleared this variable
  47.     ;   ESC_pressed  - 1 if ESC key is down, 0 if it's not
  48.     ;
  49.     ;   Active_Keys  - an array of 128 bytes, each representing the scan
  50.     ;                   code of a key. If that key is down that byte will
  51.     ;                   be a 1, if not, it will be a 0.
  52.     ────────────────────────────────────────────────────────────────────
  53. PROC Int_09 FAR
  54.     push    ax bx cx
  55.  
  56.     cli                         ;make sure no interrupts interrupt us =)
  57.     call    KB_Wait
  58.     in      al,60h              ;Grab the key...
  59.     sti                         ;allow interrupts again
  60.  
  61.     mov     ah,1                ;assume we are turning a key on
  62.     or      al,al               ;if the high bit is set, it's a release code 
  63.     jns     @@Off_Code
  64.     xor     ah,ah
  65. @@Off_Code:
  66.     and     al,01111111b                ;mask out top bit
  67.     movzx   bx,al                       ;use scan code as index
  68.     mov     [cs:Active_Keys + bx],ah    ;move in a 0 or 1
  69.     or      [cs:Key_Pressed],ah         ;update Key_Pressed
  70.  
  71.     mov     [cs:Last_Pressed],al        ;update last_pressed
  72.  
  73.     dec     al                  ;was it the ESC key? (scan code = 1)
  74.     jne     @@Not_ESC
  75.     mov     [cs:ESC_Pressed],ah ;update ESC_Pressed
  76. @@Not_ESC:
  77.  
  78.     mov     al,20h
  79.     out     20h,al
  80.     pop     cx bx ax
  81.     iret
  82. ENDP
  83.  
  84.     ────────────────────────────────────────────────────────────────────
  85.     ;   bx = interrupt # to replace                                    
  86.     ;cs:si = address of where to store old interrupt vector            
  87.     ;  eax = address of new interrupt (segment high, offset low)       
  88.     ────────────────────────────────────────────────────────────────────
  89. PROC Install_Int
  90.     push    ds cx bx eax
  91.                   
  92.     xor     cx,cx
  93.     mov     ds,cx
  94.     shl     bx,2
  95.  
  96.     cli
  97.     xchg    [ds:bx],eax
  98.     sti
  99.     mov     [cs:si],eax
  100.  
  101.     pop     eax bx cx ds
  102.     ret
  103. ENDP
  104.  
  105.     ────────────────────────────────────────────────────────────────────
  106.     ;   bx = interrupt # to restore
  107.     ;cs:si = address of where old interrupt vector was stored
  108.     ────────────────────────────────────────────────────────────────────
  109. PROC Restore_Int
  110.     push    ds ecx bx
  111.  
  112.     xor     cx,cx
  113.     mov     ds,cx
  114.     shl     bx,2
  115.  
  116.     mov     ecx,[cs:si]
  117.     cli
  118.     mov     [ds:bx],ecx
  119.     sti
  120.     
  121.     pop     bx ecx ds
  122.     ret
  123. ENDP
  124.  
  125.     ────────────────────────────────────────────────────────────────────
  126.     ; sets all the cute little KB lights to whats in BL
  127.     ────────────────────────────────────────────────────────────────────
  128. PROC setkb NEAR
  129.     push    ax
  130.     mov     al,0edh
  131.     call    Send_KB
  132.     mov     al,bl
  133.     call    Send_KB
  134.     mov     al,0f4h
  135.     call    Send_KB
  136.     pop     ax
  137.     ret
  138. ENDP
  139.  
  140.     ────────────────────────────────────────────────────────────────────
  141.     ;   Sends a command to the KB
  142.     ────────────────────────────────────────────────────────────────────
  143. PROC Send_KB NEAR
  144.     push    ax bx cx
  145.  
  146.     mov     ah,al
  147.     mov     bh,3
  148.  
  149. @@Loop_0:
  150.     call    KB_Wait
  151.     mov     al,ah
  152.     out     60h,al
  153.     
  154.     mov     cx,4000
  155.  
  156. @@Loop_1:
  157.     in      al,61h
  158.     test    al,10h
  159.     jz      @@Loop_1
  160.  
  161. @@Loop_2:
  162.     in      al,61h
  163.     test    al,10h
  164.     jnz     @@Loop_2
  165.  
  166.     in      al,64h
  167.     test    al,1
  168.     loopz   @@Loop_1
  169.  
  170.     in      al,60h
  171.     cmp     al,0fah
  172.     je      @@Done
  173.  
  174.     dec     bh
  175.     jnz     @@Loop_0
  176. @@Done:
  177.  
  178.     pop     cx bx ax
  179.     ret
  180. ENDP
  181.